package com.ruoyi.project.web.carouselInfo.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.carouselInfo.domain.CarouselInfo;
import com.ruoyi.project.web.carouselInfo.service.ICarouselInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 首页轮播Controller
 * 
 * @author ruoyi
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/web/carouselInfo")
public class CarouselInfoController extends BaseController
{
    private String prefix = "web/carouselInfo";

    @Autowired
    private ICarouselInfoService carouselInfoService;

    @RequiresPermissions("web:carouselInfo:view")
    @GetMapping()
    public String carouselInfo()
    {
        return prefix + "/carouselInfo";
    }

    /**
     * 查询首页轮播列表
     */
    @RequiresPermissions("web:carouselInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarouselInfo carouselInfo)
    {
        startPage();
        List<CarouselInfo> list = carouselInfoService.selectCarouselInfoList(carouselInfo);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播列表
     */
    @RequiresPermissions("web:carouselInfo:export")
    @Log(title = "首页轮播", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarouselInfo carouselInfo)
    {
        List<CarouselInfo> list = carouselInfoService.selectCarouselInfoList(carouselInfo);
        ExcelUtil<CarouselInfo> util = new ExcelUtil<CarouselInfo>(CarouselInfo.class);
        return util.exportExcel(list, "carouselInfo");
    }

    /**
     * 新增首页轮播
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存首页轮播
     */
    @RequiresPermissions("web:carouselInfo:add")
    @Log(title = "首页轮播", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile imgFile, CarouselInfo carouselInfo) throws IOException {
        if (imgFile != null) {
            // 上传文件路径
            String photoPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String photoSaveName = FileUploadUtils.upload(photoPath, imgFile);
            carouselInfo.setImgUrl(photoSaveName);
        } else {
            return error("图片未上传");
        }
        return toAjax(carouselInfoService.insertCarouselInfo(carouselInfo));
    }

    /**
     * 修改首页轮播
     */
    @GetMapping("/edit/{carouselId}")
    public String edit(@PathVariable("carouselId") Long carouselId, ModelMap mmap)
    {
        CarouselInfo carouselInfo = carouselInfoService.selectCarouselInfoById(carouselId);
        mmap.put("carouselInfo", carouselInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页轮播
     */
    @RequiresPermissions("web:carouselInfo:edit")
    @Log(title = "首页轮播", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarouselInfo carouselInfo)
    {
        return toAjax(carouselInfoService.updateCarouselInfo(carouselInfo));
    }

    /**
     * 删除首页轮播
     */
    @RequiresPermissions("web:carouselInfo:remove")
    @Log(title = "首页轮播", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carouselInfoService.deleteCarouselInfoByIds(ids));
    }
    /*验证排序号重复*/
    @PostMapping( "/checkSortNumUnique")
    @ResponseBody
    public int checkSortNumUnique(CarouselInfo carouselInfo)
    {
        return carouselInfoService.checkSortNumUnique(carouselInfo);
    }
}
